require("../../assets/css/index.less");
require("./myinfo.less");
require("../../assets/fonts/iconfont.css");
const Swiper = require("../../lib/swiper/swiper-bundle.js");
require("../../lib/swiper/swiper-bundle.css");
let dom = require("../../utils/dom.js");
let https = require("../../utils/https.js");
let weui = require('../../lib/weui/weui.js')
let local = require('../../utils/local')


dom.ready(function () {
    dom.getEle('.backBtn').addEventListener('click', function () {
        window.history.go(-1)
    })
    //头像上传
    let imgUrl;
    dom.getEle('#headFile').addEventListener('change', function () {
        let f = this.files[0];
        let fd = new FormData();
        fd.append('file', f)
        https.upLoadPic('/api/shared/uploadPortrait1', fd, function (res) {
            console.log(res);
            if (res.errno == 0) {
                avatar.src = 'http://47.96.154.185:3701' + res.data.url;
                imgUrl=res.data.url
            }
        })
    })

    //更换性别
    let gender;
    dom.getEle('#sex').addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                // console.log(result);
                dom.getEle('#sexTxt').innerText = result[0].label
                gender = result[0].value
            },
            title: '请选择性别'
        });
    })

    //选择日期
    dom.getEle('#birthday').addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                // console.log(result);
                let str = result.map(function (v) {
                    return v.value < 10 ? '0' + v.value : v.value
                }).join('-')
                dom.getEle('#birthday').innerText = str;
            },
            title: '多列选择器'
        });
    })

    //选择省份
    let provinceId;
    dom.getEle('#provincebtn').addEventListener('click', function () {
        https.get('/api/shared/province', {}, function (res) {
            // console.log(res);
            if (res.errno == 0) {
                let provinceData = res.data.map(function (v) {
                    // console.log(v)
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                // console.log(provinceData)
                weui.picker(provinceData, {
                    onConfirm: function (result) {
                        // console.log(result);
                        dom.getEle('#provinceTxt').innerText = result[0].label;

                        provinceId = result[0].value;
                        // console.log(provinceId)
                    },
                    title: '请选择省份'
                });
            }
        })
    })
    let cityId;
    dom.getEle('#city').addEventListener('click', function () {
        https.get('/api/shared/city', { provinceId }, function (res) {
            if (res.errno == 0) {
                let cityArr = res.data.map(function (v) {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                // console.log(cityArr);
                weui.picker(cityArr, {
                    onConfirm: function (result) {
                        dom.getEle('#cityTxt').innerText = result[0].label
                        cityId = result[0].value;
                    },
                    title: '请选择城市'
                })
            }
        })
    })

    //输入框
    dom.getEle('#info').addEventListener('input', function () {
        // console.log(this.value.length)
        let len = this.value.length;
        dom.getEle('#minnum').innerText = len;
        if (len > 200) {
            dom.getEle('#info').value = (dom.getEle('#info').value).slice(0, 199)
            dom.getEle('#minnum').innerText = 200
        }
    })

    //保存
    dom.getEle('.save-btn').addEventListener('click', function () {
        let data = {
            imgUrl: imgUrl ? imgUrl : local.get('userData').imgUrl,
            nickName: (nickName.value ? nickName.value : local.get('userData').nickName) ? nickName.value ? nickName.value : local.get('userData').nickName : '匿名',
            gender,
            birthday: birthday.innerText,
            provinceId,
            cityId,
            sign: info.value
        }
           console.log(data)
        // 发ajax   
        https.post('/api/user/changeInfo', data, function (res) {
            console.log(res)
            if (res.errno == 0) {
                weui.toast('修改成功', 3000);
                //存储到本地
                localStorage.setItem('tk', res.data.token);
                //跳转个人页面
                window.location.href = '/mine.html'
            }
        })
    })
    //数据回填功能
    function render(){
        //1.获取本地数据 
        let localData=local.get('userData');

        //2.回填数据到网页中
        avatar.src='http://47.96.154.185:3701' +localData.imgUrl;
        nickName.value=localData.nickName;
        birthday.innerText=localData.birthday;
        sexTxt.innerText=localData.gender=='0'?'男':localData.gender=='1'?'女':'不详';
        info.value=localData.sign;

        // provincebtn.innerText=returnCitySN.cname;
        // provinceId=returnCitySN.cid
        console.log(localData);
    }

    render()

})
